/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.utils');

goog.require('goog.math');

/**
 * Transforms time in seconds into string separating hours, minutes and seconds.
 * For instance "1:12:03", "19:05:03", "12:03", "08:03", "00:03".
 * @param {number} totalTime total time in seconds.
 * @return {string} Human-readable time.
 */
kramerius.audio.utils.secondsToTime = function(totalTime){
    //console.log("totalTime (s):" + totalTime);
    var result = "";
    var hours = Math.floor(totalTime/3600);
    //console.log("hours:" + hours);
    if (hours != 0){
        result = result + hours + ":";
    }
    var timeWithoutHours = goog.math.modulo(totalTime, 3600);
    //console.log("timeWithoutHours (s):" + timeWithoutHours);
    var minutes = Math.floor(timeWithoutHours/60);
    //console.log("minutes:" + minutes);
    if (hours != 0){
        result += kramerius.audio.utils.toTwoOrMoreDigits_(minutes) + ":"; 
    } else{
        result += minutes + ":";
    }
    var seconds = Math.floor(goog.math.modulo(timeWithoutHours, 60));
    //result += this.toTwoOrMoreDigits_(seconds);
    result += kramerius.audio.utils.toTwoOrMoreDigits_(seconds);
    //console.log("seconds:" + seconds);
    //console.log("result:" + seconds);
    return result;
}

/**
 * Appends '0' before the number in case it is single-digit number.
 * @param {number} number number to be transformed
 * @return {string} number with preceeding '0' if necessary.
 * @private
 */
kramerius.audio.utils.toTwoOrMoreDigits_ = function(number){
    if (number>=10){
        return number;
    }else{
        return "0" + number;
    }
}

/**
 * Determines whethet two numbers absolute difference is higher than specified treshold.
 * @param {number} first first number 
 * @param {number} second second number 
 * @param {number} minDiff minimal difference (treshold)
 * @return {boolean} True if difference is higher than treshold.
 */
kramerius.audio.utils.differsByAtLeast = function(first, second, minDiff){
    var diff = Math.abs(first - second);
    //TODO: odstranit
    //console.log("diff: " + diff + " (" + first + ', ' + second + ')');
    return diff > minDiff;
}

//Copyright (c) 2007 John Resig
//
//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:
//
//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.
kramerius.audio.utils.removeArray = function(array, from, to) {
    ////Found at http://ejohn.org/blog/javascript-array-remove/
    ////Examples:
    //// Remove the second item from the array
    //array.remove(1);
    //// Remove the second-to-last item from the array
    //array.remove(-2);
    //// Remove the second and third items from the array
    //array.remove(1,2);
    //// Remove the last and second-to-last items from the array
    //array.remove(-2,-1);
    var rest = array.slice((to || from) + 1 || array.length);
    array.length = from < 0 ? array.length + from : from;
    return array.push.apply(array, rest);
};


